package util;

import entity.Users;

import java.io.*;
import java.util.List;
import java.util.Map;

public class Oprea {
    //序列化员工
    public static void seria(Map map, File file){
        ObjectOutputStream oos=null;
        try {
            oos=new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(map);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(oos!=null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //反序列化员工
    public static Map unseria(File file){
        ObjectInputStream ois=null;
        Map map=null;
        if(file.exists()){
            try {
                ois=new ObjectInputStream(new FileInputStream(file));
                map= (Map) ois.readObject();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }finally {
                if(ois!=null){
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return map;
    }
    //序列化用户
    public static void seriaUser(List list, File file){
        ObjectOutputStream oos=null;
        try {
            oos=new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(list);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(oos!=null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //反序列化用户
    public static List unseriaUser(File file){
        ObjectInputStream ois=null;
        List list=null;
        if(file.length() > 0){
            try {
                ois=new ObjectInputStream(new FileInputStream(file));
                list= (List) ois.readObject();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }finally {
                if(ois!=null){
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list;
    }
}
